//账号/企业切换
$('body>ol>li').click(function () {
    var index = $(this).index();
    $(this).addClass('selected').siblings().removeClass('selected');
    $('body>ul>li').eq(index).addClass('selected').siblings().removeClass('selected');
});
//编辑或保存
$('.accinfo>b').click(function () {
    $(this).siblings('div').slideDown().prev('h3').slideUp();
    if ($(this).text() == "编辑") {
        $(this).text("保存");
    }
});
$('.accinf>b').click(function () {
    if($('#apply').attr("sort")!=2){
    $(this).siblings('div').slideDown().prev('h3').slideUp();
    if ($(this).text() == "编辑") {
        $(this).text("保存");
    }
    }
});
//点击申请
if ($('#apply').attr("sort") == "1" || $('#apply').attr("sort") == "3") {
    $('#apply').text("申请中").css({background: "#b7b7b7"}).attr("sort", "1");
}
$('#apply').click(function () {
    if ($(this).attr("sort") == ""||$(this).attr("sort") == "1" || $(this).attr("sort") == "3"||$(this).attr("sort") =="0") {
        $('body>ol li').eq(1).click();
        $(window).scrollTop(0);
        $(this).text("申请中").css({background: "#b7b7b7"}).attr("sort", "1");
        new Ajax("/my/apply.jsp").get(function () {});
        $(window.parent.document).find('#hint').html('<img src="../img/dui.png"/><span>&emsp;正在申请请完善资料</span>').parent().show();
    }else{
        
    }
});
//判断是否为申请
var sort = $('#apply').attr("sort");
switch (sort) {
    case "1":
        $('#apply').css({background: "#b7b7b7"}).text("申请中");
        $('#apply').siblings('span').hide();
        $('#apply').siblings('h3').hide();
        $('#apply').siblings('div').show();
        break;
    case "2":
        $('#apply').css({background: "#b7b7b7"}).text("已通过");
        $("#apply").next().next().text("恭喜您已经成为供应商！");
        $('#apply').siblings('h3').hide();
        break;
    case "3":
        $('#apply').text("重新申请");
}
//验证文本输入框
var info = new Validated();
info.rules = {
    "linkname": {
        "data": /^\S+$/,
        "complete": "",
        "error": "请输入姓名！",
        "normal": "",
        "required": true
    },
    "pwd": {
        "data": /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
        "complete": "",
        "error": "密码为6-20位数字字母组合！",
        "normal": "",
        "required": true
    },
    "pwd2": {
        "data": /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
        "complete": "",
        "error": "密码为6-20位数字字母组合！",
        "normal": "",
        "required": true
    },
    "telphone": {
        "data": /^1[3578]\d{9}$/,
        "complete": "",
        "error": "请输入正确的手机号码！",
        "normal": "",
        "required": true
    },
    "yzm": {
        "data": /^\d{6}$/,
        "complete": "",
        "error": "验证码规则错误！",
        "normal": "",
        "required": true
    },
    "yzm2": {
        "data": /^\d{6}$/,
        "complete": "",
        "error": "验证码规则错误！",
        "normal": "",
        "required": true
    },
    "email": {
        "data": /^\w+@\w+\.\w+$/,
        "complete": "",
        "error": "请输入正确的邮箱号码！",
        "normal": "",
        "required": true
    },
    "pwd3": {
        "data": /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
        "complete": "",
        "error": "密码为6-20位数字字母组合！",
        "normal": "",
        "required": true
    },
    "newPwd": {
        "data": /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
        "complete": "",
        "error": "密码为6-20位数字字母组合！",
        "normal": "",
        "required": true
    },
    "ackPwd": {
        "data": function () {
            if ($('#newPwd').val() == $('#ackPwd').val()) {
                return true;
            }
        },
        "complete": "",
        "error": "密码为6-20位数字字母组合！",
        "normal": "",
        "required": true
    },
    "account": {
        "data": /^\w{6,30}$/,
        "complete": "",
        "error": "账号为6-30位数字字母下划线组成！",
        "normal": "",
        "required": true
    },
    "pwd4": {
        "data": /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
        "complete": "",
        "error": "密码为6-20位数字字母组合！",
        "normal": "",
        "required": true
    },
    "name": {
        "data": /^\S+$/,
        "complete": "",
        "error": "请输入姓名！",
        "normal": "",
        "required": true
    },
    "phone": {
        "data": /^1[3578]\d{9}$/,
        "complete": "",
        "error": "请输入正确的手机号码！",
        "normal": "",
        "required": true
    },
    "position": {
        "data": /^.+$/,
        "complete": "",
        "error": "请输入工作岗位！",
        "normal": "",
        "required": true
    },
    "els": {
        "data": /^.+$/,
        "complete": "",
        "error": "请输入正确的els账号！",
        "normal": "",
        "required": true
    },
    "companyName": {
        "data": /^.+$/,
        "complete": "",
        "error": "请输入公司名称！",
        "normal": "",
        "required": true
    },
    "category": {
        "data": /^.+$/,
        "complete": "",
        "error": "",
        "normal": "",
        "required": true
    },
    "product": {
        "data": /^.+$/,
        "complete": "",
        "error": "",
        "normal": "",
        "required": true
    },
//    "companyAddr": {
//        "data": /^.+$/,
//        "complete": "",
//        "error": "请输入企业网址！",
//        "normal": "",
//        "required": true
//    },
    "person": {
        "data": /^.+$/,
        "complete": "",
        "error": "请输入联系人姓名！",
        "normal": "",
        "required": true
    },
    "mail": {
        "data": /^\w+@\w+\.\w+$/,
        "complete": "",
        "error": "请输入正确的邮箱号码！",
        "normal": "",
        "required": true
    },
    "tel": {
        "data": /^1[3578]\d{9}$/,
        "complete": "",
        "error": "请输入正确的手机号码！",
        "normal": "",
        "required": true
    },
    "Telephone": {
        "data": /^\S+$/,
        "complete": "",
        "error": "请输入正确的联系电话！",
        "normal": "",
        "required": true
    },
    "credit": {
        "data": /^[A-Z0-9]{15,18}$/g,
        "complete": "",
        "error": "由18位数字与大写字母组成！",
        "normal": "",
        "required": true
    },
    "juristicPerson": {
        "data": /^\S{1,20}$/,
        "complete": "",
        "error": "请填写法定代表姓名！",
        "normal": "",
        "required": true
    },
    "atm": {
        "data": /^\S{1,20}$/,
        "complete": "",
        "error": "请输入正确的开户行！",
        "normal": "",
        "required": true
    },
    "cardNumber": {
        "data": /^\S{15,20}$/,
        "complete": "",
        "error": "请输入正确的开户行卡号！",
        "normal": "",
        "required": true
    },
    "capital": {
        "data": /^[\d.]+$/,
        "complete": "",
        "error": "金额规则为数字类型！",
        "normal": "",
        "required": true
    },
    "licence": {
        "data": /^[A-Z0-9]{15,18}$/g,
        "complete": "",
        "error": "请输入正确的营业执照号码！",
        "normal": "",
        "required": true
    },
    "banklogin": {
        "data": /^\d{15}$/,
        "complete": "",
        "error": "由15位数字组成！",
        "normal": "",
        "required": true
    },
    "organization": {
        "data": /^\S{13,20}$/,
        "complete": "",
        "error": "请填写正确的组织机构代码证！",
        "normal": "",
        "required": true
    },
    "ratepaying": {
        "data": /^\S{13,20}$/,
        "complete": "",
        "error": "请填写正确的纳税人识别号！",
        "normal": "",
        "required": true
    },
    "address": {
        "data": /^\S{1,20}$/,
        "complete": "",
        "error": "详细地址不可以包含空格！",
        "normal": "",
        "required": true
    }
}
info.blur();

//短信发倒计时
var telCheck = true, t = 60;
$('.accinfo .button').eq(0).click(function () {
    if (info.complete("telphone")) {
        var button = $(this);
        if (telCheck) {
            var s = setInterval(function () {
                t--;
                if (t < 1) {
                    clearInterval(s);
                    button.text("重新发送");
                    t = 60;
                    telCheck = true;
                    return false;
                }
                button.text(t + "秒后重新发送");
            }, 1000);
            new Ajax("/login/seccode.jsp").keyvalue("phone", $('#telphone').val()).get();
        }
        telCheck = false;
    }

});
//邮箱倒计时
var emailCheck = true, n = 60;
$('.accinfo .button').eq(1).click(function () {
    if (info.complete("email")) {
        var button = $(this);
        if (emailCheck) {
            var s = setInterval(function () {
                n--;
                if (n < 1) {
                    clearInterval(s);
                    button.text("重新发送");
                    n = 60;
                    emailCheck = true;
                    return false;
                }
                button.text(n + "秒后重新发送");
            }, 1000);
            new Ajax("/login/secemail.jsp").keyvalue("email", $('#email').val()).get();
        }
        emailCheck = false;
    }
});

//初始化省级数据
$.each(AreaListData, function (k, v) {
    if (v[1] == 0) {
        $('#province').append('<option value="' + k + '">' + v[0] + '</option>');
    }
});
//初始化市级数据
$.each(AreaListData, function (k, v) {
    if (v[1] == 1) {
        $('#city').append('<option value="' + k + '">' + v[0] + '</option>');
    }
});
//改变/添加 市级数据
$('#province').change(function () {
    $('#city').empty();
    var provinceId = $(this).val();
    $.each(AreaListData, function (k, v) {
        if (v[1] == provinceId) {
            $('#city').append('<option value="' + k + '">' + v[0] + '</option>');
        }
    })
});
//改变/添加 县区级数据
//$('#city').change(function () {
//    $('#county').empty();
//    var cityId = $(this).val();
//    $.each(AreaListData, function (k, v) {
//        if (v[1] == cityId) {
//            $('#county').append('<option value="' + k + '">' + v[0] + '</option>');
//        }
//    });
//    if ($('#county option').length == 0) {
//        $('#county').hide();
//    } else {
//        $('#county').show();
//    }
//});

//日历
//$(function () {
//    $.datepicker.regional['zh-CN'] = {
//        changeYear: true,
//        monthNamesShort: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
//        dayNamesMin: ['日', '一', '二', '三', '四', '五', '六']
//    };
//    $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
//    $("#defult").datetimepicker();
//    $("#found").prop("readonly", true).datepicker({
//        changeMonth: true,
//        dateFormat: "yy-mm-dd",
//    });
//    $("#start").prop("readonly", true).datepicker({
//        changeMonth: true,
//        dateFormat: "yy-mm-dd",
//    });
//    $("#end").prop("readonly", true).datepicker({
//        changeMonth: true,
//        dateFormat: "yy-mm-dd",
//    });
//});
//父级窗口
//var hint = $(window.parent.document).find('#vague #hint');
//var img = hint.find('img');
//联系人保存
$('#lxr').click(function () {
    var lxr = $(this).siblings('div').find("input").val();
    if (lxr.length > 0&& lxr!="请填写您的姓名") {
        new Ajax("account_edit.jsp").keyvalue({"linkname": lxr, "type": "lxr"}).post(function (data) {
            if (data == 1) {
                $(window.parent.document).find('#hint').html('<img src="../img/dui.png"/><span>&emsp;保存成功！</span>').parent().show();
            }
        });
    }
});
//手机号修改
$('#shouji').click(function () {
    if (info.complete("pwd", "telphone", "yzm")) {
        new Ajax("account_edit.jsp").keyvalue({"pwd": $('#pwd').val(), "telphone": $('#telphone').val(), "yzm": $('#yzm').val(), "type": "sjh"}).post(function (data) {
            if (data == 1) {
                $(window.parent.document).find('#hint').html('<img src="../img/dui.png"/><span>&emsp;保存成功！</span>').parent().show();
            }
        });
    }
});
//邮箱号修改
$('#email_edit').click(function () {
    if (info.complete("pwd2", "email", "yzm2")) {
        new Ajax("account_edit.jsp").keyvalue({"pwd": $('#pwd2').val(), "email": $('#email').val(), "yzm": $('#yzm2').val(), "type": "yxh"}).post(function (data) {
            if (data ==1) {
            $(window.parent.document).find('#hint').html('<img src="../img/dui.png"/><span>&emsp;保存成功！</span>').parent().show();
            }
        });
    }
});
//修改密码
$('#pwd_edit').click(function () {
    if (info.complete("pwd3", "newPwd", "ackPwd")) {
        new Ajax("account_edit.jsp").keyvalue({"oldPwd": $('#pwd3').val(), "newPwd": $('#newPwd').val(), "pwd": $('#ackPwd').val(), "type": "mm"}).post(function (data) {
           if (data ==1) {
            $(window.parent.document).find('#hint').html('<img src="../img/dui.png"/><span>&emsp;保存成功！</span>').parent().show();
            }
            if (data ==5) {
            $(window.parent.document).find('#hint').html('<img src="../img/cuo.png"/><span>&emsp;原密码错误！</span>').parent().show();
            }
        });
    }
});
//新增子帐号
$('#addAccount').click(function () {
    if (info.complete("account", "pwd4", "name", "phone", "position")) {
        new Ajax("account_edit.jsp").keyvalue({"enterprise":$('#enterprise').val(),"account": $('#account').val(), "pwd": $('#pwd4').val(), "linkname": $('#name').val(), "telphone": $('#phone').val(), "posts": $('#position').val(), "type": "zzh"}).post(function (data) {
            if (data == 7) {
            $(window.parent.document).find('#hint').html('<img src="../img/cuo.png"/><span>&emsp;账号已存在！</span>').parent().show();
            }
            if (data ==8) {
            $(window.parent.document).find('#hint').html('<img src="../img/dui.png"/><span>&emsp;保存成功！</span>').parent().show();
            }
        });
    }
});
//企业资料提交
$('#sub').click(function () {
    var mainproducts = "";
    $('#mainClass input').each(function () {
        mainproducts += "," + $(this).val();
    })
    mainproducts = mainproducts.substr(1);
    var mustInfo = {
        'corpname': $('#companyName').val(),
        'mainproducts': mainproducts,
        'linkname': $('#person').val(),
        'email': $('#mail').val(),
        'telphone': $('#tel').val(),
        'fulladress':$('#address').val()
    }
    var isEmpty = true;
    $.each(mustInfo, function (k, v) {
        if (v.length == 0) {
            $(window.parent.document).find('#hint').html('<img src="../img/cuo.png"/><span>&emsp;必填项未填完！</span>').parent().show();
            isEmpty = false;
            return false;
        }
    });
    var data = $('form').serializeArray();
    data.push({'name': 'mainbusiness', 'value': mainproducts});
    if (isEmpty) {
        new Ajax("/my/account_perfect.jsp").keyvalue(data).get(function (json) {
            if (json == 1) {
                $(window.parent.document).find('#hint').html('<img src="../img/dui.png"/><span>&emsp;保存成功！</span>').parent().show();
            }
        });
    }
});
//性别选择
var sel = $('#sel').attr("sel")
if (sel == "1") {
    $("input[name='sex']:eq(0)").attr("checked", "checked");
} else {
    $("input[name='sex']:eq(1)").attr("checked", "checked");
}
//弹窗提示
function popup(text) {
    hint.empty();
    parent.show();
    text = text.replace(/[^0-9]/ig, "");
    switch (text) {
        case "1":
            hint.html("<img src='../img/dui.png'/><span>&nbsp&nbsp修改成功！</span>");
            break;
        case "2":
            hint.html("<img src='../img/cuo.png'/><span>&nbsp&nbsp修改失败！</span>");
            break;
        case "3":
            hint.html("<img src='../img/cuo.png'/><span>&nbsp&nbsp验证码有误！</span>");
            break;
        case "4":
            hint.html("<img src='../img/cuo.png'/><span>&nbsp&nbsp密码错误！</span>");
            break;
        case "5":
            hint.html("<img src='../img/cuo.png'/><span>&nbsp&nbsp原始密码错误！</span>");
            break;
        case "6":
            hint.html("<img src='../img/cuo.png'/><span>&nbsp&nbsp新旧密码不一致！</span>");
            break;
        case "7":
            hint.html("<img src='../img/cuo.png'/><span>&nbsp&nbsp账号已存在！</span>");
            break;
        case "8":
            hint.html("<img src='../img/dui.png'/><span>&nbsp&nbsp添加成功！</span>");
            break;
    }
}
$('#schema').change(function () {
    judge($(this).val());
})
//判断是否为三证合一
function judge(j) {
    if (j == 2) {
        $('#credit').parent().parent().hide();
        $('.code').show();
    } else {
        $('#credit').parent().parent().show();
        $('.code').hide();
    }
}

//选择主营类别
$('#one').change(function () {
    var val = parseInt($(this).val());
    if (!isNaN(val)) {
        new Ajax("/businessclass").keyvalue('parendid', val).getJson(function (data) {
            $('#two option').eq(0).nextAll().remove();
            $.each(data, function (k, v) {
                $('#two option').eq(0).after('<option>' + v.category + '</option>');
            });
        });
    }
});
$('#two').change(function () {
    var val = $(this).val();
    if (val != "请选择") {
        $('#mainClass').append('<span><input value="' + val + '" readOnly="true"/><em>Ｘ</em></span>');
    }
    $('#mainClass em').on('click', function () {
        $(this).parent().remove();
    });
});

//对于项目类别解析
mainproducts = $('#mainproducts').val();
var str = new Array();
str = mainproducts.split(",");
if(str[0]!=""){
$.each(str,function(k,v){
     $('#mainClass').append('<span><input value="' + v + '" readOnly="true"/><em>Ｘ</em></span>');
})
$('#mainClass em').click(function(){
    $(this).parent().remove();
})
}